/**
 * Copyright 2007 Luke Pillow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orsomethinglikethat.hinge.asgen;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.apt.AnnotationProcessors;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;

/**
 * @author lpillow
 */
public class AS3ClassAnnotationProcessorFactory implements AnnotationProcessorFactory
{
    private static final Logger log = Logger.getLogger( AS3ClassAnnotationProcessorFactory.class );

    private static final Collection<String> supportedAnnotations = Collections.singletonList( "com.orsomethinglikethat.hinge.asgen.AS3Class" );
    // private static final Collection<String> supportedAnnotations =
    // Collections.unmodifiableCollection( Arrays.asList( "*" ) );

    // Supported options
    private static final Collection<String> supportedOptions;
    static final String USER_CLASSES = "user_classes_home";
    static final String LOG4J_CONFIGURATION = "log4j.configuration";

    private static Boolean loggingConfigured = Boolean.FALSE;

    private static ActionScriptClassGenerator classGenerator;

    static
    {
        supportedOptions = new ArrayList<String>();
        supportedOptions.add( USER_CLASSES );
        classGenerator = new SimpleAS3ClassGenerator();
    }

    private void initializeLogging( AnnotationProcessorEnvironment env )
    {
        if ( !loggingConfigured )
        {
            String log4jPropsPath = env.getOptions().get( AS3ClassAnnotationProcessorFactory.LOG4J_CONFIGURATION );

            if ( log4jPropsPath != null && log4jPropsPath.length() > 0 )
            {
                String configType = log4jPropsPath.substring( log4jPropsPath.lastIndexOf( "." ) + 1, log4jPropsPath.length() );

                if ( configType.equalsIgnoreCase( "xml" ) )
                {
                    DOMConfigurator.configure( log4jPropsPath );
                    log.debug( "Logging configured via DOMConfigurator.configure(" + log4jPropsPath + " )" );
                }
                else
                {
                    PropertyConfigurator.configure( log4jPropsPath );
                    log.debug( "Logging configured via PropertyConfigurator.configure(" + log4jPropsPath + " )" );
                }

                loggingConfigured = Boolean.TRUE;
            }

        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sun.mirror.apt.AnnotationProcessorFactory#getProcessorFor(java.util.Set,
     *      com.sun.mirror.apt.AnnotationProcessorEnvironment)
     */
    public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> declarations, AnnotationProcessorEnvironment env )
    {
        this.initializeLogging( env );

        AnnotationProcessor processor = AnnotationProcessors.NO_OP;

        if ( declarations.isEmpty() == false )
        {
            processor = new AS3ClassAnnotationProcessor( env, AS3ClassAnnotationProcessorFactory.classGenerator );
        }

        return processor;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sun.mirror.apt.AnnotationProcessorFactory#supportedAnnotationTypes()
     */
    public Collection<String> supportedAnnotationTypes()
    {
        return AS3ClassAnnotationProcessorFactory.supportedAnnotations;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sun.mirror.apt.AnnotationProcessorFactory#supportedOptions()
     */
    public Collection<String> supportedOptions()
    {
        return AS3ClassAnnotationProcessorFactory.supportedOptions;
    }

}
